package work.mediway.mdm.controller.hierarchy;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.mediway.hos.base.model.BaseResponse;
import com.mediway.hos.log.annotation.OperLog;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import work.mediway.mdm.core.enums.OpsLogTypeEnum;
import work.mediway.mdm.core.page.QueryRequest;
import work.mediway.mdm.biz.entity.hierarchy.HosBusinessUnit;
import work.mediway.mdm.biz.service.hierarchy.HosBusinessUnitService;
import work.mediway.mdm.core.annotation.OpsLog;
import work.mediway.mdm.core.transport.ErrorMsgVO;

@Slf4j
@Api(tags = "组织架构-业务单元")
@ApiSupport(order = 20)
@RequestMapping("/businessUnit")
@RestController
@Validated
@RequiredArgsConstructor
public class HosBusinessUnitController {

    private final HosBusinessUnitService hosBusinessUnitService;


    @ApiOperation(value = "新增业务单元")
    @ApiOperationSupport(order = 67)
    @OpsLog(value = "新增业务单元", type = OpsLogTypeEnum.INSERT)
    @OperLog(title = "组织架构-业务单元", content = "新增业务单元")
    @PostMapping
    public BaseResponse<HosBusinessUnit> save(@Valid HosBusinessUnit hosBusinessUnitVO) {
        this.hosBusinessUnitService.saveHosBusinessUnit(hosBusinessUnitVO);
        return BaseResponse.success();
    }

    @ApiOperation(value = "查询业务单元")
    @ApiOperationSupport(order = 67)
    @OpsLog(value = "查询业务单元", type = OpsLogTypeEnum.SELECT)
    @GetMapping
    public BaseResponse<Page<HosBusinessUnit>> find(HosBusinessUnit hosBusinessUnit, QueryRequest queryRequest) {
        Page<HosBusinessUnit> result = this.hosBusinessUnitService.findHosBusinessUnit(hosBusinessUnit, queryRequest);
        return BaseResponse.success(result);
    }

    @ApiOperation(value = "更新业务单元")
    @ApiOperationSupport(order = 67)
    @OpsLog(value = "更新业务单元", type = OpsLogTypeEnum.UPDATE)
    @OperLog(title = "组织架构-业务单元", content = "更新业务单元")
    @PutMapping
    public BaseResponse<HosBusinessUnit> update(@Valid HosBusinessUnit hosBusinessUnitVO) {
        this.hosBusinessUnitService.updateHosBusinessUnit(hosBusinessUnitVO);
        return BaseResponse.success();
    }

    @ApiOperation(value = "删除业务单元")
    @ApiOperationSupport(order = 67)
    @ApiImplicitParam(name = "businessUnitId", value = "业务单元BU_Code[多个以,分隔]", paramType = "path")
    @OpsLog(value = "删除业务单元", type = OpsLogTypeEnum.DELETE)
    @OperLog(title = "组织架构-业务单元", content = "删除业务单元")
    @DeleteMapping("/{businessUnitId}")
    public BaseResponse<ErrorMsgVO> delete(@PathVariable("businessUnitId") @NotBlank(message = "不能为空") String businessUnitId) {
        ErrorMsgVO msgVO = this.hosBusinessUnitService.deleteHosBusinessUnit(businessUnitId);
        return BaseResponse.success(msgVO);
    }

    @ApiOperation(value = "查询业务单元类型")
    @ApiOperationSupport(order = 67)
    @OpsLog(value = "查询业务单元类型", type = OpsLogTypeEnum.SELECT)
    @GetMapping("type")
    public BaseResponse<List<HosBusinessUnit>> listHosBusinessUnitType() {
        List<HosBusinessUnit> result = this.hosBusinessUnitService.listHosBusinessUnitType();
        return BaseResponse.success(result);
    }

    /*@ApiOperation(value = "导出业务单元")
    @ApiOperationSupport(order = 67)
    @OpsLog(value = "导出业务单元", type = OpsLogTypeEnum.DOWNLOAD)
    @OperLog(title = "组织架构-业务单元", content = "导出业务单元")
    @GetMapping("/export")
    public AjaxResult export(HosBusinessUnit hosBusinessUnitVO) {
        List<HosBusinessUnit> positions = this.hosBusinessUnitService.findHosBusinessUnit(hosBusinessUnitVO, new QueryRequest(1, Integer.MAX_VALUE)).getRecords();
        ExcelUtil<HosBusinessUnit> util = new ExcelUtil<>(HosBusinessUnit.class);
        return util.exportExcel(positions, "业务单元");
    }

    @ApiOperation(value = "导入业务单元")
    @ApiOperationSupport(order = 67)
    @OpsLog(value = "导入业务单元", type = OpsLogTypeEnum.UPLOAD)
    @OperLog(title = "组织架构-业务单元", content = "导入业务单元")
    @PostMapping("/import")
    public R<HosBusinessUnit> uploadHosBusinessUnit(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<HosBusinessUnit> util = new ExcelUtil<>(HosBusinessUnit.class);
        List<HosBusinessUnit> businessUnits = util.importExcel(file.getInputStream());
        ErrorMsgVO result = this.hosBusinessUnitService.importHosBusinessUnit(businessUnits, updateSupport);
        return R.success(JSONUtil.toJsonStr(result));
    }

    @ApiOperation(value = "导出业务单元模板")
    @ApiOperationSupport(order = 67)
    @OpsLog(value = "导出业务单元模板", type = OpsLogTypeEnum.DOWNLOAD)
    @OperLog(title = "组织架构-业务单元", content = "导出业务单元模板")
    @GetMapping("/template")
    @ResponseBody
    public AjaxResult hosBusinessUnitTemplate() {
        ExcelUtils<HosBusinessUnit> util = new ExcelUtil<>(HosBusinessUnit.class);
        return util.importTemplateExcel("业务单元模板");
    }*/
}
